$(document).ready(function(){
	
	loadAll();
	$("#btn_simpan").click(function(data){
		$.post('controller/cont.batal.php',$("#frm_batal").serialize(),function(data){
            if (data.msg) {
                if (data.edit) {
                    goAdd();
                  	$("#nip_detail").html(data.cuti.nip);
                  	$("#nama_detail").html(data.pegawai.nama);
                  	$("#tgl_detail").html(data.cuti.tglAwal+" s.d. "+data.cuti.tglAkhir);
                  	$("#jns_detail").html(data.ref);
                    document.getElementById("tgl_awal").value = formattedDate(data.cuti.tglAwal);
                    document.getElementById("tgl_akhir").value = formattedDate(data.cuti.tglAkhir);
                    document.getElementById("frm_aksi").value = "simpan_edit";
                    document.getElementById("saveButton").className = "buttonshort";
                    document.getElementById("undoButton").className = "buttonshort";
                    document.getElementById("editButton").className = "buttonshortdis";
                } else {
                    document.getElementById("sukses").innerHTML = data.sukses;
                    loadAll();
                    $("#div_sukses").fadeIn(500).delay(3500).fadeOut(500);
                    goUndo();
                }
            } else {
                document.getElementById("error").innerHTML = data.eror;
                $("#div_error").fadeIn(500).delay(3500).fadeOut(500);
                goUndo();
            }
            return false;
        },'json');
        return false;
	});
});

function formattedDate(date) {
    var d = new Date(date || Date.now()),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [day, month, year].join('-');
}

function loadAll(){
	$.getJSON('controller/cont.cuti.php',{aksi_get_toBatal:true},function(data){
		var tab='';
		$('#list_cuti tbody').html('');
		if(data.msg){
				$.each(data.cuti, function(index, cuti) {
					tab += "<tr>";
	                tab += "<td>" + cuti.pegawai.nama + "</td>";
	                tab += "<td>" + cuti.pegawai.nip + "</td>";
	                tab += "<td>" + cuti.cuti.tglAju + "</td>";
	                tab += "<td>" + cuti.cuti.tglAwal + " s.d. " + cuti.cuti.tglAkhir + "</td>";
	                tab += "<td>" + cuti.cuti.jmlHari + "</td>";
	                tab += "<td>" + cuti.ref + "</td>";
	                tab += "<td>" + cuti.proses.nama + "</td>";
	                tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" +cuti.cuti.id + "\"/></td>";
	                tab += "</tr>";
	            });
	            
		}else{
			tab += "<tr>"
            tab += "<td>Data Tidak Ada</td>";
            tab += "</tr>";
		}
		$('#list_cuti tbody').append(tab);
	});
}